{extend name='base'/}
{block name='title'}修改资料{/block}
{block name='main'}
<link rel="stylesheet" href="__CSS__/booklist.css">
<style>
    .main {
        background: url("__IMG__/list_bg.jpg") left top repeat-y;
    }
    form {
        width: 60%;
    }
    form label{
        font-size: 14px;
    }
    select{
        width: 50% !important;
    }
</style>
<div class="row m-0 mt-3 main">
    {include file='user/user_left'}
    <div class="right flex-1">
        <form class="address-form mb-3">
            <div class="form-group">
                <label for="truename">收货人 <span class="text-danger">*</span></label>
                <input type="text"  name="data[truename]" class="form-control" id="truename" value="{$address.truename}">
            </div>
            <div class="form-group">
                <label>所在地区 <span class="text-danger">*</span></label>
                <div class="d-flex">
                    {$html|raw}
                </div>
            </div>
            <div class="form-group">
                <label for="address">详细地址 <span class="text-danger">*</span></label>
                <input type="text"  name="data[address]" class="form-control" id="address" value="{$address.address}">
            </div>
            <div class="form-group">
                <label for="mobile">手机号 <span class="text-danger">*</span></label>
                <input type="text"  name="data[mobile]" class="form-control" id="mobile" value="{$address.mobile}">
            </div>
            <input type="hidden" name="id" value="{$id}">
            <button type="button" class="btn btn-primary editaddr-btn">立即提交</button>
        </form>
    </div>
</div>
{/block}

{block name='js'}
<script>
    let UPLOADURL = "{:url('common/upload/index')}";
</script>
<script src="__JS__/upload.js"></script>
<script>
    $(function () {
        let lock = false;
        $(".editaddr-btn").on('click', function () {
           if (lock) {
               return false;
           }
           lock = true;
           $.post("{:url('User/myaddress')}", $(".address-form").serialize(), function (res) {
               if (res.code == 0) {
                   swal("提示", res.msg, {
                       icon : "error",
                       buttons: {
                           confirm: {
                               className : 'btn btn-danger',
                               text : '确定',
                           }
                       },
                   });
               }else{
                   swal("提示", res.msg, {
                       icon : "success",
                       buttons: {
                           confirm: {
                               className : 'btn btn-success',
                               text : '确定',
                           }
                       }
                   }).then(function () {
                       window.location.replace("{:url('User/address')}");
                   });
               }
               lock = false;
           }, 'json');
        });

        $("#province").on('change', function () {
           var areaid = $(this).val();
           if (areaid == 0) {
               return false;
           }
           $.get("{:url('common/area/getarea')}?areaid="+areaid, function (res) {
               console.log(res)
               var html = '<select name="data[areaid]" id="city" class="form-control"><option value="0">请选择</option>';
               for (var i = 0; i < res.data.length; i++) {
                   html += '<option value="'+res.data[i].areaid+'">'+res.data[i].areaname+'</option>';
               }
               html += '</select>';
               $("#city").remove();
               $('#province').parent().append(html);
           }, 'json');
        });
    });
</script>
{/block}